package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.PartDAO;
import edu.byu.intex2.data.dao.OrderDAO;
import edu.byu.intex2.data.dao.OrderLineDAO;
import edu.byu.intex2.data.dao.PartsAtServiceFacilityDAO;

/**
 * @author Jason Whiting
 */
public class OrderLine extends BusinessObject
{
    private int quantity;
    private String orderId;
    private Order order;
    private String partId;
    private Part part;
    private PartsAtServiceFacility psf;

    /**
     * Creates a new instance of BusinessObject
     * @param id
     */
    public OrderLine(String id)
    {
        super(id);
    }//constructor

    //Quick Access Method
    public void save() throws DataException
    {
        try
        {
            OrderLineDAO.getInstance().save(this);
        } catch (DataException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * @return the quantity
     */
    public int getQuantity()
    {
        return quantity;
    }

    /**
     * @param actualPrice the actualPrice to set
     */
    public void setQuantity(int quantity)
    {
        this.quantity = quantity;
        setDirty();
    }

    /**
     * @return the orderId
     */
    public String getOrderId()
    {
        return orderId;
    }

    /**
     * @param orderId the orderId to set
     */
    public void setOrderId(String orderId)
    {
        this.orderId = orderId;
        setDirty();
    }

    /**
     * @return the order
     */
    public Order getOrder() throws Exception
    {
        if (order == null)
        {
            order = OrderDAO.getInstance().read(orderId);
        }
        return order;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(Order order)
    {
        this.order = order;
        setDirty();
    }

    /**
     * @return the partId
     */
    public String getPartId()
    {
        if (partId == null)
        {
            partId = part.getId();
        }
        return partId;
    }

    /**
     * @param partId the partId to set
     */
    public void setPartId(String partId)
    {
        this.partId = partId;
        setDirty();
    }

    /**
     * @return the part
     */
    public Part getPart()
    {
        if (part == null)
        {
            try
            {
                part = PartDAO.getInstance().read(partId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return part;
    }

    /** @param part the part to set     */
    public void setPart(Part part)
    {
        this.part = part;
        setDirty();
    }

    /**
     * @return the psf
     */
    public PartsAtServiceFacility getPsf()
    {
        if (psf == null)
        {
            try
            {
                PartsAtServiceFacilityDAO.getInstance().read(getOrder().getServiceFacilityId(), partId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return psf;
    }

    /**
     * @param psf the psf to set
     */
    public void setPsf(PartsAtServiceFacility psf)
    {
        this.psf = psf;
    }
}
